using System;
using AndyPike.Snowflake.Core.Exceptions;
using AndyPike.Snowflake.Core.Messages;
using AndyPike.Snowflake.Entities;
using NUnit.Framework;
using Rhino.Mocks;

namespace AndyPike.Snowflake.Core.Tests.Unit.Services.AccountServiceFixtures
{
    [TestFixture]
    public class When_retrieving_a_principal_from_an_authentication_token : AccountServiceFixtureBase
    {
        [Test]
        public void Should_leverage_the_repository_to_check_that_the_user_name_is_unique()
        {
            Guid authenticationToken = Guid.NewGuid();
            var account = mockery.Stub<Account>();
            
            With.Mocks(mockery)
                .Expecting(delegate
                               {
                                   Expect.Call(accountRepository.FindById(authenticationToken)).Return(account);
                                   SetupResult.For(service.AccountToAccountInfoMessageMapper.Map(account)).Return(new AccountInfoMessage());
                               })
                .Verify(() => service.RetrievePrincipleFromAuthenticationToken(authenticationToken));
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_throw_an_exception_if_the_authentication_token_is_empty()
        {
            service.RetrievePrincipleFromAuthenticationToken(Guid.Empty);
        }

        [Test]
        [ExpectedException(typeof(InvalidAuthenticationTokenException))]
        public void Should_throw_an_exception_if_the_authentication_token_is_invalid()
        {
            Guid authenticationToken = Guid.NewGuid();

            With.Mocks(mockery)
                .Expecting(() => Expect.Call(accountRepository.FindById(authenticationToken)).Return(null))
                .Verify(() => service.RetrievePrincipleFromAuthenticationToken(authenticationToken));
        }
    }
}
